from flask_wtf import FlaskForm
from wtforms.fields import simple
from wtforms.fields import html5


from wtforms import widgets,validators

class RegisterFM(FlaskForm):
    name = simple.StringField(
        validators=[
            validators.DataRequired()
        ],
        widget=widgets.TextInput(),
        render_kw={'class': 'form-control','placeholder':"请输入用户名"}

    )

    pwd = simple.PasswordField(
        validators=[
            validators.DataRequired(message='密码不能为空.'),
            validators.Regexp(regex="^(?=.*[a-z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{6,}",
                              message='密码至少6个字符，至少1个小写字母，1个数字和1个特殊字符')
        ],
        widget=widgets.PasswordInput(),
        render_kw={'class': 'form-control','placeholder':"请输入密码，至少6个字符，并包含字母和特殊字符"}
    )

    pwd_confirm = simple.PasswordField(
        validators=[
            validators.DataRequired(message='重复密码不能为空.'),
            validators.EqualTo('pwd', message="两次密码输入不一致")
        ],
        widget=widgets.PasswordInput(),
        render_kw={'class': 'form-control ','placeholder':"请输入再次输入密码"}
    )





    def __init__(self, *args, **kwargs):
        super(RegisterFM, self).__init__(*args, **kwargs)


    def validate_pwd_confirm(self, field):
        """
        自定义pwd_confirm字段规则，例：与pwd字段是否一致
        :param field:
        :return:
        """
        # 最开始初始化时，self.data中已经有所有的值

        if field.data != self.data['pwd']:
            # raise validators.ValidationError("密码不一致") # 继续后续验证
            raise validators.StopValidation("密码不一致")  # 不再继续后续验证
